package com.diapoc.diorsgallery;

import java.io.ByteArrayOutputStream;
import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.diapoc.diorsgallery.utils.DownloadManager;
import com.diapoc.diorsgallery.utils.LogManager;

public class ActivityGridPic extends Activity {

	private Context context;

	private GridView gv_class2;
	private ArrayList<PicHolder> bitmaps;
	private MyBaseAdapter mba;

	Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			mba.updateBitmaps(bitmaps);
			mba.notifyDataSetChanged();
			super.handleMessage(msg);
		}

	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_grid);
		init();
		initLayout();
	}

	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		startDownloadThread();
		super.onStart();
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
	}

	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}

	@Override
	protected void onRestart() {
		// TODO Auto-generated method stub

		super.onRestart();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	private void init() {
		context = this;

	}

	private void initLayout() {
		// TODO Auto-generated method stub
		gv_class2 = (GridView) findViewById(R.id.gv_class2);
		bitmaps = new ArrayList<PicHolder>();
		for (int i = 0; i < 8; i++) {
			bitmaps.add(new PicHolder());
		}
		mba = new MyBaseAdapter(context, bitmaps);
		gv_class2.setAdapter(mba);
	}

	private void startDownloadThread() {
		for (int i = 0; i < 8; i++) {
			final int final_i = i;
			new Thread() {
				public void run() {
					DownloadManager dm = new DownloadManager() {

						@Override
						public void onDownLoading(int rate) {
							// TODO Auto-generated method stub
							PicHolder picHolder = new PicHolder();
							picHolder.downloadRate = rate;
							bitmaps.set(final_i, picHolder);
							handler.sendEmptyMessage(final_i);
						}

					};
					ByteArrayOutputStream stream = dm
							.download("http://cxy.fxox.cn/" + final_i + ".jpg");
					Bitmap pic = dm.byte2Bitmap(stream.toByteArray());
					PicHolder picHolder = new PicHolder();
					picHolder.downloadRate = 100;
					picHolder.pic = pic;
					bitmaps.set(final_i, picHolder);
					handler.sendEmptyMessage(final_i);
				}
			}.start();
		}
	}

	private class PicHolder {
		int downloadRate = 0;
		Bitmap pic = null;
	}

	private class ViewHolder {
		ImageView iv_grid;
		TextView tv_id;
	}

	private class MyBaseAdapter extends BaseAdapter {

		private LayoutInflater mInflater;
		private Context context;
		private ArrayList<PicHolder> bitmaps;

		public MyBaseAdapter(Context context, ArrayList<PicHolder> bitmaps) {
			this.context = context;
			this.mInflater = LayoutInflater.from(context);
			this.bitmaps = bitmaps;
		}

		public void updateBitmaps(ArrayList<PicHolder> bitmaps) {
			this.bitmaps = bitmaps;
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return bitmaps.size();
		}

		@Override
		public Object getItem(int arg0) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public long getItemId(int arg0) {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup arg2) {
			ViewHolder holder = null;
			if (convertView == null) {

				holder = new ViewHolder();

				convertView = mInflater.inflate(R.layout.item_grid1, null);
				holder.iv_grid = (ImageView) convertView
						.findViewById(R.id.iv_grid);
				holder.tv_id = (TextView) convertView.findViewById(R.id.tv_id);
				convertView.setTag(holder);

			} else {

				holder = (ViewHolder) convertView.getTag();
			}
			oprView(position, convertView, arg2, holder);
			return convertView;
		}

		private void oprView(int position, View convertView, ViewGroup arg2,
				ViewHolder holder) {
			holder.tv_id.setText(bitmaps.get(position).downloadRate + "%");
			if (bitmaps.get(position) == null) {
				Log.v("test", position + " is null");
				holder.iv_grid.setBackgroundResource(R.drawable.ic_launcher);
			} else {
				Log.v("test", position + " is not null");
				holder.iv_grid.setImageBitmap(bitmaps.get(position).pic);
			}
		}

		private int randomLayoutId() {
			int random = (int) (Math.random() * 10 % 4);
			int layoutId = -1;
			switch (random) {
			case 0:
				layoutId = R.layout.item_grid1;
				break;
			case 1:
				layoutId = R.layout.item_grid2;
				break;
			case 2:
				layoutId = R.layout.item_grid3;
				break;
			case 3:
				layoutId = R.layout.item_grid4;
				break;
			}
			return layoutId;
		}
	}
}
